#include "stdafx.h"
#include "MiniCppUnit.hxx"
#include "leudgrid/streamblock.h"
#include "leudgrid/streamblock.inl"
#include <process.h>

const int THREAD_NUM = 2;

 LEUD::PoolMemory<LEUD::MemoryBlock<1024> >							LEUD::PoolMemoryBase<LEUD::MemoryBlock<1024> >::memPool;
 LEUD::PoolMemory<LEUD::MemoryBlock<2048> >							LEUD::PoolMemoryBase<LEUD::MemoryBlock<2048> >::memPool;
 LEUD::PoolMemory<LEUD::MemoryBlock<8> >								LEUD::PoolMemoryBase<LEUD::MemoryBlock<8> >::memPool;

unsigned int __stdcall TestBlockThread( void* data )
{
	LEUD::MemoryBlock<1024>* A1024[1000];
	LEUD::MemoryBlock<2048>* A2048[1000];
	for( int j = 0;j < 500; j ++ )
	{
		for( int i = 0; i < 1000; i ++ )
		{
			A1024[i] = new LEUD::MemoryBlock<1024>;
		}
		for( int i = 0; i < 1000; i ++ )
		{
			A2048[i] = new LEUD::MemoryBlock<2048>;
		}
		for( int i = 0; i < 1000; i ++ )
		{
			delete A2048[i];
		}
		for( int i = 0; i < 1000; i ++ )
		{
			delete A1024[i];
		}
	}
	return 0;
}

class TestStreamBlock : public TestFixture<TestStreamBlock>
{
public:
	TEST_FIXTURE( TestStreamBlock )
	{
		TEST_CASE( InOuttest );
		TEST_CASE( noneTest );
		TEST_CASE( buffertest );
		TEST_CASE( cleartest );
		TEST_CASE( strendwithtest );
	}

void InOuttest()
	{
		LEUD::StreamBlock< LEUD::MemoryBlock<8> > stream;
		char c1 = 10;
		int a = 100;
		short b = 101;
		char c2 = 11;
		int c = 102;
		short d = 103;
		char c3 = 12;
		int e = 104;
		short f = 105;
		char c4 = 13;
		std::string buf = "12345678901234567890123456789012";
		std::string buffer = buf;
		std::string bufferb = buf;
		stream<<c1<<a<<b<<c2<<c<<d<<c3<<e<<f<<c4;
		stream.In((char*)buffer.c_str(),buffer.size());
		//stream<<bufferb;
		a=b=c=d=e=f=0;
		stream>>c1>>a>>b>>c2>>c>>d>>c3>>e>>f>>c4;
		char bb[1000];
		memset(bb,0,1000);
		stream.Out(bb,buf.size());
		buffer = (char*)bb;
		//stream>>bufferb;
		stream.seek(0);
		a=b=c=d=e=f=0;
		stream>>c1>>a>>b>>c2>>c>>d>>c3>>e>>f>>c4;
		memset(bb,0,1000);
		stream.Out(bb,buf.size());
		buffer = (char*)bb;
		//stream>>bufferb;
		ASSERT(c1==10);
		ASSERT(a==100);
		ASSERT(b==101);
		ASSERT(c2==11);
		ASSERT(c==102);
		ASSERT(d==103);
		ASSERT(c3==12);
		ASSERT(e==104);
		ASSERT(f==105);
		ASSERT(c4==13);
		ASSERT(buf == buffer );
		ASSERT(buf == bufferb );
	}
	void noneTest()
	{
		HANDLE m_hThread[THREAD_NUM];
		UINT   ThreadID;
		for (int i = 0; i < THREAD_NUM; i++)
		{
			m_hThread[i] = (HANDLE)_beginthreadex(NULL, 0, TestBlockThread, 0, 0, &ThreadID );
		}
		WaitForMultipleObjects(THREAD_NUM, m_hThread, TRUE, INFINITE);

		ASSERT(1);
	}
	void buffertest()
	{
		LEUD::StreamBlock< LEUD::MemoryBlock<1024> > stream;
		int i = 100;
		short s = 101;
		stream<<i<<s;
		i = 0;
		s = 0;
		stream>>i>>s;
		ASSERT(i == 100);
		ASSERT(s == 101);
	}
	void cleartest()
	{
		LEUD::StreamBlock< LEUD::MemoryBlock<8> > stream;
		int a = 10;
		int b = 20;
		int c = 30;
		short aa = 100;
		short bb = 200;
		short cc = 300;
		char aaa = 1;
		char bbb = 2;
		char ccc = 3;
		stream<<a<<aa<<aaa<<b<<bb<<bbb<<c<<cc<<ccc;
		a=b=c=0;
		aa=bb=cc=0;
		aaa=bbb=ccc=0;
		stream>>a;
		stream.clear(sizeof(a));
		stream>>aa;
		stream.clear(sizeof(aa));
		stream>>aaa;
		stream.clear(sizeof(aaa));
		stream>>b;
		stream.clear(sizeof(b));
		stream>>bb;
		stream.clear(sizeof(bb));
		stream>>bbb;
		stream.clear(sizeof(bbb));
		stream>>c;
		stream.clear(sizeof(c));
		stream>>cc;
		stream.clear(sizeof(cc));
		stream>>ccc;
		stream.clear(sizeof(ccc));
		ASSERT(a	== 10);
		ASSERT(aa	== 100);
		ASSERT(aaa	== 1);
		ASSERT(b	== 20);
		ASSERT(bb	== 200);
		ASSERT(bbb	== 2);
		ASSERT(c	== 30);
		ASSERT(cc	== 300);
		ASSERT(ccc	== 3);
	}
	void strendwithtest()
	{
		//LEUD::StreamBlock< LEUD::MemoryBlock<8> > stream;
		//std::string str = "012345678901234567890123456789";
		//stream<<str;
		//int i=0;
		//stream>>i;
		//std::string str1;
		//stream.Out(str1,'0');
		//ASSERT(str1 == "0");
		//stream.Out(str1,'0');
		//ASSERT(str1 == "1234567890" );
		//stream.Out(str1,'0');
		//ASSERT(str1 == "1234567890" );
		//stream.Out(str1,'0');
		//ASSERT(str1 == "");
	}
};

REGISTER_FIXTURE( TestStreamBlock );


